/* eslint-disable @typescript-eslint/no-var-requires */
// webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const friendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { name } = require("./package.json");

module.exports = {
  target: 'web', // 解决找不到fs模块的问题
  mode: 'development',
  devtool: 'inline-source-map',
  entry: {
    vendor: ["react", "antd", "react-dom", "react-router-dom", "react-router-config", "react-redux", "redux"],
    app: './src/index.tsx'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'scripts/[name].[fullhash:8].js'
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.scss', '.css'],
    alias: {
      "@": path.resolve(__dirname, "src"),
      "@assets": path.resolve(__dirname, "src", "assets"),
      "@components": path.resolve(__dirname, "src", "components"),
      "@pages": path.resolve(__dirname, "src", "pages"),
      "@services": path.resolve(__dirname, "src", "services"),
      "@helpers": path.resolve(__dirname, "src", "helpers"),
      "@interfaces": path.resolve(__dirname, "src", "interfaces"),
      "@config": path.resolve(__dirname, "src", "config"),
      "@constants": path.resolve(__dirname, "src", "constants"),
      "@routes": path.resolve(__dirname, "src", "routes"),
      "@store": path.resolve(__dirname, "src", "store"),
      "@hooks": path.resolve(__dirname, "src", "hooks"),
      "@types": path.resolve(__dirname, "src", "types"),
    }
  },
  plugins: [
    new HtmlWebpackPlugin({
      inject: 'body',
      template: path.resolve(__dirname, 'public', 'index.ejs'),
      filename: 'index.html',
      favicon: path.resolve(__dirname, 'public', 'favicon.ico'),
      templateParameters: {
        title: name
      },
    }),
    new CleanWebpackPlugin({
      dry: false,
      cleanStaleWebpackAssets: true,
    }),
    new MiniCssExtractPlugin({
      filename: "styles/[name].[fullhash:8].css", // 打包到css文件夹里面
      chunkFilename: "[id].css"
    }),
    new friendlyErrorsWebpackPlugin(),
  ],
  module: {
    rules: [
      // JavaScript
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
          },
        },
      },
      // Images
      {
        test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
        loader: 'url-loader'
      },
      // Fonts and SVGs
      {
        test: /\.(woff|woff2|eot|ttf|otf|svg|)$/,
        loader: 'url-loader'
      },
      // CSS, PostCSS, and Sass
      {
        test: /\.(scss|css)$/i,
        use: [
          {
            loader: MiniCssExtractPlugin.loader
          },
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1
            }
          },
          {
            loader: 'postcss-loader',
          },
          // 解决这个报错的问题
          // [0] Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
          // [0] ModuleBuildError: Module build failed (from ./node_modules/css-loader/dist/cjs.js):
          // [0] Error: Can't resolve 'iconfo.woff?t=1619232089330' in 'D:\workspace\react-wallpaper\src\assets\iconfont'
          // 'resolve-url-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.ejs$/,
        loader: 'ejs-loader',
        options: {
          esModule: false
        },
        exclude: /node_modules/,
      }
    ],
  },
  node: {
    __dirname: true,
  },
  devServer: {
    historyApiFallback: true,
    contentBase: path.join(__dirname, 'dist'),
    open: false,
    hot: true,
    quiet: true,
    port: 8000,
    // publicPath: '/dist/'
  },
  optimization: {
    splitChunks: {
      chunks: "all",
      minSize: 0,
      maxSize: 1024 * 100, // 单位: kb 每个打包出来的文件的最大尺寸, 写的越小,打出来的包越多
      minChunks: 1, // 表示被引用次数，默认为1, 被引用1次的代码抽离到公共代码里面
    }
  }
}
